from django.views import View

# 导入解析用户信息函数
# 获取数据库连接
from SaaSDevelTool.data_mysql_data import db
# 判断梯队位置
from SaaSDevelTool.data_operation import is_number
# 导入返回响应
from SaaSDevelTool.data_return_response import create_response
# 导入错误信息装饰器
from SaaSDevelTool.data_return_response import error_message_detection

"""功能包梯队位置添加"""


class SaaSDevelModelPackAgeLevelQuery(View):
    @error_message_detection
    def get(self, request):
        # 获取大模块编号
        md_id = request.GET.get("md_id")
        # 判断大模块编号
        if not is_number(md_id):
            return create_response(code=400, msg="大模块编号传递错误。")

        # 根据大模块进行接口获取
        query_sum_package_level_sql = """
            SELECT 
                group_concat(distinct l.echelon_name) as echelon_name,
                group_concat(spt.package_name) as package_name,
                group_concat(DISTINCT l.echelon_level) as echelon_level,
                group_concat(DISTINCT spt.model_id) as model_id  
            FROM 
                db_saas_center.sum_package_level l
            LEFT JOIN  
                db_saas_center.sum_package_table spt
            ON 
                l.echelon_package_id = spt.package_id 
            WHERE 
                l.md_id = {} 
            GROUP BY   
                l.echelon_level, l.echelon_name,spt.model_id 
        ;""".format(md_id)
        # 调用语句进行查询
        query_sum_package_level_data = db.findAll(query_sum_package_level_sql)
        # 返回对应的响应语句
        return create_response(code=200, msg="", data=query_sum_package_level_data)
